package com.wysql.ascost.controller.bigscreen;//package com.wysql.fx.controller;

import com.wysql.ascost.model.R;
import com.wysql.ascost.model.bigscreen.ProjectData;
import com.wysql.ascost.model.info.*;
import com.wysql.ascost.service.info.InfoProjectbudgetCsService;
import com.wysql.ascost.service.info.InfoProjectbudgetJsService;
import com.wysql.ascost.service.info.InfoProjectbudgetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @name: BigScreenController
 * @author: wysql
 * @date: 2025-06-25 10:40
 */
@Controller
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/BigScreenController")
@CrossOrigin
public class BigScreenController {

    //    private String prefix = "kanban";
    private final InfoProjectbudgetService infoProjectbudgetService;
    private final InfoProjectbudgetCsService infoProjectbudgetCsService;
    private final InfoProjectbudgetJsService infoProjectbudgetJsService;

    @PostMapping("getImportData")
    @ResponseBody
    public ResponseEntity<R> getImportData(@RequestParam String year) {
        Map<String, Object> resultMap = new HashMap<>();

        BigDecimal percentall = BigDecimal.ZERO;

        //可研阶段
        List<InfoProjectbudget> budgets = infoProjectbudgetService.selectByExample(new InfoProjectbudgetExample());
        List<InfoProjectbudget> infoProjectbudgets = budgets.stream().filter(s -> s.getBookdate().startsWith(year)).collect(Collectors.toList());
        List<InfoProjectbudget> infoProjectbudgetsCon = infoProjectbudgets.stream().filter(s -> s.getIsconfirm() == 1).collect(Collectors.toList());
        int countky = infoProjectbudgets.size();
        resultMap.put("countky", countky);
        BigDecimal moneyky = infoProjectbudgets.stream().map(InfoProjectbudget::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP);
        resultMap.put("moneyky", moneyky);
        List<Map<String, Object>> kyList = genetateProDataList();
        List<InfoProjectbudget> prodataList = infoProjectbudgetService.selectProData(year);
        Map<Integer, InfoProjectbudget> prodataMap = prodataList.stream().collect(Collectors.toMap(InfoProjectbudget::getType, s -> s, (existing, replacement) -> existing));
        for (Map<String, Object> map : kyList) {
            Integer typenum = (Integer) map.get("typenum");
            if (prodataMap.containsKey(typenum)) {
                InfoProjectbudget infoProjectbudget = prodataMap.get(typenum);
                map.put("procount", infoProjectbudget.getProcount());
                map.put("totalmoney", infoProjectbudget.getTotalmoney().divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP));
                map.put("batchcount", infoProjectbudget.getBatchcount());
            }
        }
        resultMap.put("kyList", kyList);

        //初设阶段
        List<InfoProjectbudgetCs> budgetsCs = infoProjectbudgetCsService.selectByExample(new InfoProjectbudgetCsExample());
        List<InfoProjectbudgetCs> infoProjectbudgetsCs = budgetsCs.stream().filter(s -> s.getBookdate().startsWith(year)).collect(Collectors.toList());
        List<InfoProjectbudgetCs> infoProjectbudgetsCsCon = infoProjectbudgetsCs.stream().filter(s -> s.getIsconfirm() == 1).collect(Collectors.toList());
        int countcs = infoProjectbudgetsCs.size();
        resultMap.put("countcs", countcs);
        BigDecimal moneycs = infoProjectbudgetsCs.stream().map(InfoProjectbudgetCs::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP);
        resultMap.put("moneycs", moneycs);
        List<Map<String, Object>> csList = genetateProDataList();
        List<InfoProjectbudgetCs> prodataListcs = infoProjectbudgetCsService.selectProData(year);
        Map<Integer, InfoProjectbudgetCs> prodataMapCs = prodataListcs.stream().collect(Collectors.toMap(InfoProjectbudgetCs::getType, s -> s, (existing, replacement) -> existing));
        for (Map<String, Object> map : csList) {
            Integer typenum = (Integer) map.get("typenum");
            if (prodataMapCs.containsKey(typenum)) {
                InfoProjectbudgetCs infoProjectbudgetCs = prodataMapCs.get(typenum);
                map.put("procount", infoProjectbudgetCs.getProcount());
                map.put("totalmoney", infoProjectbudgetCs.getTotalmoney().divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP));
                map.put("batchcount", infoProjectbudgetCs.getBatchcount());
            }
        }
        resultMap.put("csList", csList);

        //结算阶段
        List<InfoProjectbudgetJs> budgetsJs = infoProjectbudgetJsService.selectByExample(new InfoProjectbudgetJsExample());
        List<InfoProjectbudgetJs> infoProjectbudgetsJs = budgetsJs.stream().filter(s -> s.getBookdate().startsWith(year)).collect(Collectors.toList());
        List<InfoProjectbudgetJs> infoProjectbudgetsJsCon = infoProjectbudgetsJs.stream().filter(s -> s.getIsconfirm() == 1).collect(Collectors.toList());
        int countjs = infoProjectbudgetsJs.size();
        resultMap.put("countjs", countjs);
        BigDecimal moneyjs = infoProjectbudgetsJs.stream().map(InfoProjectbudgetJs::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP);
        resultMap.put("moneyjs", moneyjs);
        List<Map<String, Object>> jsList = genetateProDataList();
        List<InfoProjectbudgetJs> prodataListjs = infoProjectbudgetJsService.selectProData(year);
        Map<Integer, InfoProjectbudgetJs> prodataMapJs = prodataListjs.stream().collect(Collectors.toMap(InfoProjectbudgetJs::getType, s -> s, (existing, replacement) -> existing));
        for (Map<String, Object> map : jsList) {
            Integer typenum = (Integer) map.get("typenum");
            if (prodataMapJs.containsKey(typenum)) {
                InfoProjectbudgetJs infoProjectbudgetJs = prodataMapJs.get(typenum);
                map.put("procount", infoProjectbudgetJs.getProcount());
                map.put("totalmoney", infoProjectbudgetJs.getTotalmoney().divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP));
                map.put("batchcount", infoProjectbudgetJs.getBatchcount());
            }
        }
        resultMap.put("jsList", jsList);

        //全环节项目
        Set<String> pro1 = infoProjectbudgetsCon.stream().map(InfoProjectbudget::getPronum).collect(Collectors.toSet());
        Set<String> pro2 = infoProjectbudgetsCsCon.stream().map(InfoProjectbudgetCs::getPronum).collect(Collectors.toSet());
        Set<String> pro3 = infoProjectbudgetsJsCon.stream().map(InfoProjectbudgetJs::getPronum).collect(Collectors.toSet());
        pro1.retainAll(pro2);
        pro1.retainAll(pro3);
        int countall = pro1.size();
        resultMap.put("countall", countall);

        if (countky > 0) {
            percentall = new BigDecimal(countall).multiply(new BigDecimal(100)).divide(new BigDecimal(countky), 0, RoundingMode.HALF_UP);
        }
        resultMap.put("percentall", percentall);

        return ResponseEntity.ok(R.returnOk().data(resultMap));
    }

    @PostMapping("getRangeData")
    @ResponseBody
    public ResponseEntity<R> getRangeData(@RequestParam String year) {
        Map<String, Object> resultMap = new HashMap<>();
        //可研与初设对比区间List
        List<Map<String, Object>> kycompareList = genetateCompareDataList();
        List<InfoProjectbudget> kyandcsList = infoProjectbudgetService.selectRangeData(year);
        List<InfoProjectbudget> kyandcsListrange1 = kyandcsList.stream().filter(s -> s.getComparepercent().compareTo(BigDecimal.ZERO) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(2)) < 0).collect(Collectors.toList());
        List<InfoProjectbudget> kyandcsListrange2 = kyandcsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(2)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(5)) < 0).collect(Collectors.toList());
        List<InfoProjectbudget> kyandcsListrange3 = kyandcsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(5)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(10)) < 0).collect(Collectors.toList());
        List<InfoProjectbudget> kyandcsListrange4 = kyandcsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(10)) >= 0).collect(Collectors.toList());

        Map<Integer, Long> kyandcsListrange1Map = kyandcsListrange1.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudget::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandcsListrange2Map = kyandcsListrange2.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudget::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandcsListrange3Map = kyandcsListrange3.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudget::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandcsListrange4Map = kyandcsListrange4.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudget::getType,
                        Collectors.counting()
                ));

        for (Map<String, Object> map : kycompareList) {
            Integer rangenum = (Integer) map.get("rangenum");
            if (rangenum==1){
                map.put("jjcount", kyandcsListrange1Map.get(1)==null?0:kyandcsListrange1Map.get(1));
                map.put("ykcount", kyandcsListrange1Map.get(2)==null?0:kyandcsListrange1Map.get(2));
                map.put("jgcount", kyandcsListrange1Map.get(3)==null?0:kyandcsListrange1Map.get(3));
                map.put("dxcount", kyandcsListrange1Map.get(4)==null?0:kyandcsListrange1Map.get(4));
            } else if (rangenum==2){
                map.put("jjcount", kyandcsListrange2Map.get(1)==null?0:kyandcsListrange2Map.get(1));
                map.put("ykcount", kyandcsListrange2Map.get(2)==null?0:kyandcsListrange2Map.get(2));
                map.put("jgcount", kyandcsListrange2Map.get(3)==null?0:kyandcsListrange2Map.get(3));
                map.put("dxcount", kyandcsListrange2Map.get(4)==null?0:kyandcsListrange2Map.get(4));
            } else if (rangenum==3){
                map.put("jjcount", kyandcsListrange3Map.get(1)==null?0:kyandcsListrange3Map.get(1));
                map.put("ykcount", kyandcsListrange3Map.get(2)==null?0:kyandcsListrange3Map.get(2));
                map.put("jgcount", kyandcsListrange3Map.get(3)==null?0:kyandcsListrange3Map.get(3));
                map.put("dxcount", kyandcsListrange3Map.get(4)==null?0:kyandcsListrange3Map.get(4));
            } else if (rangenum==4){
                map.put("jjcount", kyandcsListrange4Map.get(1)==null?0:kyandcsListrange4Map.get(1));
                map.put("ykcount", kyandcsListrange4Map.get(2)==null?0:kyandcsListrange4Map.get(2));
                map.put("jgcount", kyandcsListrange4Map.get(3)==null?0:kyandcsListrange4Map.get(3));
                map.put("dxcount", kyandcsListrange4Map.get(4)==null?0:kyandcsListrange4Map.get(4));
            }
        }

        resultMap.put("kycompareList",kycompareList);

        //初设与结算对比区间List
        List<Map<String, Object>> cscompareList = genetateCompareDataList();
        List<InfoProjectbudgetCs> csandjsList = infoProjectbudgetCsService.selectRangeData(year);
        List<InfoProjectbudgetCs> csandjsListrange1 = csandjsList.stream().filter(s -> s.getComparepercent().compareTo(BigDecimal.ZERO) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(2)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetCs> csandjsListrange2 = csandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(2)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(5)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetCs> csandjsListrange3 = csandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(5)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(10)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetCs> csandjsListrange4 = csandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(10)) >= 0).collect(Collectors.toList());

        Map<Integer, Long> csandjsListrange1Map = csandjsListrange1.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetCs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> csandjsListrange2Map = csandjsListrange2.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetCs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> csandjsListrange3Map = csandjsListrange3.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetCs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> csandjsListrange4Map = csandjsListrange4.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetCs::getType,
                        Collectors.counting()
                ));

        for (Map<String, Object> map : cscompareList) {
            Integer rangenum = (Integer) map.get("rangenum");
            if (rangenum==1){
                map.put("jjcount", csandjsListrange1Map.get(1)==null?0:csandjsListrange1Map.get(1));
                map.put("ykcount", csandjsListrange1Map.get(2)==null?0:csandjsListrange1Map.get(2));
                map.put("jgcount", csandjsListrange1Map.get(3)==null?0:csandjsListrange1Map.get(3));
                map.put("dxcount", csandjsListrange1Map.get(4)==null?0:csandjsListrange1Map.get(4));
            } else if (rangenum==2){
                map.put("jjcount", csandjsListrange2Map.get(1)==null?0:csandjsListrange2Map.get(1));
                map.put("ykcount", csandjsListrange2Map.get(2)==null?0:csandjsListrange2Map.get(2));
                map.put("jgcount", csandjsListrange2Map.get(3)==null?0:csandjsListrange2Map.get(3));
                map.put("dxcount", csandjsListrange2Map.get(4)==null?0:csandjsListrange2Map.get(4));
            } else if (rangenum==3){
                map.put("jjcount", csandjsListrange3Map.get(1)==null?0:csandjsListrange3Map.get(1));
                map.put("ykcount", csandjsListrange3Map.get(2)==null?0:csandjsListrange3Map.get(2));
                map.put("jgcount", csandjsListrange3Map.get(3)==null?0:csandjsListrange3Map.get(3));
                map.put("dxcount", csandjsListrange3Map.get(4)==null?0:csandjsListrange3Map.get(4));
            } else if (rangenum==4){
                map.put("jjcount", csandjsListrange4Map.get(1)==null?0:csandjsListrange4Map.get(1));
                map.put("ykcount", csandjsListrange4Map.get(2)==null?0:csandjsListrange4Map.get(2));
                map.put("jgcount", csandjsListrange4Map.get(3)==null?0:csandjsListrange4Map.get(3));
                map.put("dxcount", csandjsListrange4Map.get(4)==null?0:csandjsListrange4Map.get(4));
            }
        }

        resultMap.put("cscompareList",cscompareList);

        //初设与结算对比区间List
        List<Map<String, Object>> jscompareList = genetateCompareDataList();
        List<InfoProjectbudgetJs> kyandjsList = infoProjectbudgetJsService.selectRangeData(year);
        List<InfoProjectbudgetJs> kyandjsListrange1 = kyandjsList.stream().filter(s -> s.getComparepercent().compareTo(BigDecimal.ZERO) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(2)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetJs> kyandjsListrange2 = kyandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(2)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(5)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetJs> kyandjsListrange3 = kyandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(5)) >= 0 &&  s.getComparepercent().compareTo(new BigDecimal(10)) < 0).collect(Collectors.toList());
        List<InfoProjectbudgetJs> kyandjsListrange4 = kyandjsList.stream().filter(s -> s.getComparepercent().compareTo(new BigDecimal(10)) >= 0).collect(Collectors.toList());

        Map<Integer, Long> kyandjsListrange1Map = kyandjsListrange1.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetJs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandjsListrange2Map = kyandjsListrange2.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetJs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandjsListrange3Map = kyandjsListrange3.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetJs::getType,
                        Collectors.counting()
                ));
        Map<Integer, Long> kyandjsListrange4Map = kyandjsListrange4.stream()
                .collect(Collectors.groupingBy(
                        InfoProjectbudgetJs::getType,
                        Collectors.counting()
                ));

        for (Map<String, Object> map : jscompareList) {
            Integer rangenum = (Integer) map.get("rangenum");
            if (rangenum==1){
                map.put("jjcount", kyandjsListrange1Map.get(1)==null?0:kyandjsListrange1Map.get(1));
                map.put("ykcount", kyandjsListrange1Map.get(2)==null?0:kyandjsListrange1Map.get(2));
                map.put("jgcount", kyandjsListrange1Map.get(3)==null?0:kyandjsListrange1Map.get(3));
                map.put("dxcount", kyandjsListrange1Map.get(4)==null?0:kyandjsListrange1Map.get(4));
            } else if (rangenum==2){
                map.put("jjcount", kyandjsListrange2Map.get(1)==null?0:kyandjsListrange2Map.get(1));
                map.put("ykcount", kyandjsListrange2Map.get(2)==null?0:kyandjsListrange2Map.get(2));
                map.put("jgcount", kyandjsListrange2Map.get(3)==null?0:kyandjsListrange2Map.get(3));
                map.put("dxcount", kyandjsListrange2Map.get(4)==null?0:kyandjsListrange2Map.get(4));
            } else if (rangenum==3){
                map.put("jjcount", kyandjsListrange3Map.get(1)==null?0:kyandjsListrange3Map.get(1));
                map.put("ykcount", kyandjsListrange3Map.get(2)==null?0:kyandjsListrange3Map.get(2));
                map.put("jgcount", kyandjsListrange3Map.get(3)==null?0:kyandjsListrange3Map.get(3));
                map.put("dxcount", kyandjsListrange3Map.get(4)==null?0:kyandjsListrange3Map.get(4));
            } else if (rangenum==4){
                map.put("jjcount", kyandjsListrange4Map.get(1)==null?0:kyandjsListrange4Map.get(1));
                map.put("ykcount", kyandjsListrange4Map.get(2)==null?0:kyandjsListrange4Map.get(2));
                map.put("jgcount", kyandjsListrange4Map.get(3)==null?0:kyandjsListrange4Map.get(3));
                map.put("dxcount", kyandjsListrange4Map.get(4)==null?0:kyandjsListrange4Map.get(4));
            }
        }

        resultMap.put("jscompareList",jscompareList);

        return ResponseEntity.ok(R.returnOk().data(resultMap));
    }

    private List<Map<String, Object>> genetateCompareDataList() {
        List<Map<String, Object>> map = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("range", "0%~2%");
        map1.put("rangenum", 1);
        map1.put("jjcount", 0);
        map1.put("ykcount", 0);
        map1.put("jgcount", 0);
        map1.put("dxcount", 0);
        map.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("range", "2%~5%");
        map2.put("rangenum", 2);
        map2.put("jjcount", 0);
        map2.put("ykcount", 0);
        map2.put("jgcount", 0);
        map2.put("dxcount", 0);
        map.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("range", "5%~10%");
        map3.put("rangenum", 3);
        map3.put("jjcount", 0);
        map3.put("ykcount", 0);
        map3.put("jgcount", 0);
        map3.put("dxcount", 0);
        map.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("range", "10%以上");
        map4.put("rangenum", 4);
        map4.put("jjcount", 0);
        map4.put("ykcount", 0);
        map4.put("jgcount", 0);
        map4.put("dxcount", 0);
        map.add(map4);
        return map;
    }

    private List<Map<String, Object>> genetateProDataList() {
        List<Map<String, Object>> map = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("typenum", 1);
        map1.put("type", "基建类项目");
        map1.put("procount", 0);
        map1.put("totalmoney", BigDecimal.ZERO);
        map1.put("batchcount", 0);
        map.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("typenum", 2);
        map2.put("type", "业扩类项目");
        map2.put("procount", 0);
        map2.put("totalmoney", BigDecimal.ZERO);
        map2.put("batchcount", 0);
        map.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("typenum", 3);
        map3.put("type", "技改类项目");
        map3.put("procount", 0);
        map3.put("totalmoney", BigDecimal.ZERO);
        map3.put("batchcount", 0);
        map.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("typenum", 4);
        map4.put("type", "大修类项目");
        map4.put("procount", 0);
        map4.put("totalmoney", BigDecimal.ZERO);
        map4.put("batchcount", 0);
        map.add(map4);
        return map;
    }

}
